<?php
/**
 * 赞model
 */
namespace app\stream\model;

class Zan extends \think\Model
{
    
    protected $tableName = 'user_data';//user统计表
    //定义字段信息
    protected $fields = [
        0  => 'zan_remain',
        1  => 'uid',
        2  => 'zan_count'
    ];
    protected function _initialize(){
        //重置副本储存
        $this->duplicate = [];
    }
    /**
     * @name 添加收到的赞
     */
    public function zanInc($uid = 0 , $num = 1){
        if(!$uid || !model('open/User')->hasUser((int)$uid)) {
            $this->error = '用户不存在';
            return false;
        }
        if(!$this->where('uid='.(int)$uid)->find()){
            $zan_id = $this->data(['uid'=>$uid])->add();
            if(!$zan_id) {
                $this->error = '添加失败';
                return false;
            }
        }
        self::_initialize();
        if($this->where('uid='.(int)$uid)->setInc('zan_remain',$num)){
            return self::updateZanCount($uid,$num);
        }
        
    }
    
    /**
     * @name 扣除赞
     */
    public function zanDec($uid = 0 , $num = 1){
        if(!$uid || !model('open/User')->hasUser((int)$uid)) {
            $this->error = '用户不存在';
            return false;
        }
        $count = self::getZanCountByUid($uid);
        if($count <= 0 || $count < $num){
            $this->error = '所剩赞不足';
            return false;
        }
        //扣除数量
        self::_initialize();
        if($this->where('uid='.(int)$uid)->setDec('zan_remain',$num)){
            //return self::_create_order($uid);
            return true;
        }
        $this->error = '扣除赞失败';
        return false;
    }
    
    /**
     * @name 获取指定用户的赞余额
     */
    public function getZanCountByUid($uid = 0){
        if($uid){
            $zan_count = $this->getFieldByUid($uid,'zan_remain');
        }
        return (int)$zan_count?:0;
    }
    /**
     * @name 获取列表数据
     */
    public function getList($uid = 0 , $limit = 10){
        //整理用户的旧记录
        $type = 'zan';
        //拼接查询sql;
        $sql = "SELECT t.desction,t.ctime,t.num,t.uid,t.to_uid,t.trade_order,t.order_type FROM (";
        $sql .= "SELECT desction,ctime,num,uid,to_uid,trade_order,order_type FROM __PREFIX__consume_log WHERE uid = ".$uid;
        $sql .= " AND type = '".$type."' UNION ALL ";
        $sql .= "SELECT desction,ctime,num,uid,to_uid,trade_order,order_type FROM __PREFIX__profit_log WHERE to_uid = ".$uid;
        $sql .= " AND type = '".$type."') t ORDER BY t.ctime DESC";
        $sql = strtr($sql,['__PREFIX__'=>C('database.prefix')]);
        $list = $this->findPageBySql($sql,null,$limit);
        if($list['gtLastPage'] === true | !$list['data']){
            return [];
        }else{
            foreach($list['data'] as &$v){
                //是否为提现?
                if($v['order_type'] == 3){
                    $v['save_status'] = (int)M('order')->getFieldByTradeOrder($v['trade_order'],'save_status')?:2;
                }else{
                    $v['save_status'] = 1;
                }
                $v['uname'] = getUname($v['uid']);
                $v['to_uname'] = getUname($v['to_uid']);
                $v['order_type'] = (int)$v['order_type'];
                $v['trade_order'] = jiami($v['trade_order']);
                unset($v['uid'],$v['to_uid']);
            }
            return $list['data'];
        }
    }
    /**
     * @name 同步更新赞的总统计数
     */
    protected function updateZanCount($uid = 0,$count = 0){
        if(!$uid || !$count) return 0;
        
        $this->where('uid='.$uid)->setInc('zan_count',$count);
        return model('open/UserData')->syncData($uid);
    }

}